# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Jun 30 2011)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc
import Database

###########################################################################
## Class UserDialog
###########################################################################

ValPermission = {0:u"管理员",
                 1:u"客户"}
PermissionVal = {u"管理员":0,
                 u"客户":1}

class UserDialog ( wx.Dialog ):
    
    def __init__( self, parent ):
        wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"用户管理", pos = wx.DefaultPosition, size = wx.Size( 600,420 ), style = wx.DEFAULT_DIALOG_STYLE )
        
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        
        bSizer30 = wx.BoxSizer( wx.VERTICAL )
        
        self.m_lcList = wx.ListCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LC_REPORT )
        self.m_lcList.InsertColumn(0, u"编号")
        self.m_lcList.InsertColumn(1, u"用户名")
        self.m_lcList.InsertColumn(2, u"权限")
        bSizer30.Add( self.m_lcList, 1, wx.ALL|wx.EXPAND, 5 )
        
        self.m_staticline16 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        bSizer30.Add( self.m_staticline16, 0, wx.EXPAND |wx.ALL, 5 )
        
        bSizer31 = wx.BoxSizer( wx.HORIZONTAL )
        
        self.m_btnNew = wx.Button( self, wx.ID_ANY, u"新建", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer31.Add( self.m_btnNew, 0, wx.ALL, 5 )
        
        self.m_btnDelete = wx.Button( self, wx.ID_ANY, u"删除", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer31.Add( self.m_btnDelete, 0, wx.ALL, 5 )
        
        self.m_btnEdit = wx.Button( self, wx.ID_ANY, u"修改", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer31.Add( self.m_btnEdit, 0, wx.ALL, 5 )
        
        self.m_btnSubmit = wx.Button( self, wx.ID_ANY, u"提交", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer31.Add( self.m_btnSubmit, 0, wx.ALL, 5 )
        
        bSizer30.Add( bSizer31, 0, wx.EXPAND, 5 )
        
        sbSizer6 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"内容" ), wx.VERTICAL )
        
        fgSizer9 = wx.FlexGridSizer( 0, 6, 0, 0 )
        fgSizer9.SetFlexibleDirection( wx.BOTH )
        fgSizer9.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
        
        self.m_staticText45 = wx.StaticText( self, wx.ID_ANY, u"编号", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText45.Wrap( -1 )
        fgSizer9.Add( self.m_staticText45, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcNo = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcNo.Enable( False )
        
        fgSizer9.Add( self.m_tcNo, 0, wx.ALL, 5 )
        
        self.m_staticText46 = wx.StaticText( self, wx.ID_ANY, u"用户名", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText46.Wrap( -1 )
        fgSizer9.Add( self.m_staticText46, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcUserName = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_tcUserName.Enable( False )
        
        fgSizer9.Add( self.m_tcUserName, 0, wx.ALL, 5 )
        
        self.m_staticText47 = wx.StaticText( self, wx.ID_ANY, u"密码", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText47.Wrap( -1 )
        fgSizer9.Add( self.m_staticText47, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcPassword = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_PASSWORD )
        self.m_tcPassword.Enable( False )
        
        fgSizer9.Add( self.m_tcPassword, 0, wx.ALL, 5 )
        
        self.m_staticText48 = wx.StaticText( self, wx.ID_ANY, u"权限", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText48.Wrap( -1 )
        fgSizer9.Add( self.m_staticText48, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        m_choice4Choices = [u""] + [k for k in PermissionVal.keys()]
        self.m_tcPermission = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( 100,-1 ), m_choice4Choices, 0 )
        self.m_tcPermission.SetSelection( 0 )
        self.m_tcPermission.Enable( False )
        
        fgSizer9.Add( self.m_tcPermission, 0, wx.ALL, 5 )
        
        
        fgSizer9.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
        
        
        fgSizer9.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
        
        self.m_staticText49 = wx.StaticText( self, wx.ID_ANY, u"密码确认", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText49.Wrap( -1 )
        fgSizer9.Add( self.m_staticText49, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5 )
        
        self.m_tcSecondPassword = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_PASSWORD )
        self.m_tcSecondPassword.Enable( False )
        
        fgSizer9.Add( self.m_tcSecondPassword, 0, wx.ALL, 5 )
        
        sbSizer6.Add( fgSizer9, 1, wx.EXPAND, 5 )
        
        bSizer30.Add( sbSizer6, 0, wx.EXPAND, 5 )
        
        self.m_staticline18 = wx.StaticLine( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
        bSizer30.Add( self.m_staticline18, 0, wx.EXPAND |wx.ALL, 5 )
        
        m_sdbSizer7 = wx.StdDialogButtonSizer()
        self.m_sdbSizer7OK = wx.Button( self, wx.ID_OK, label = u"关闭" )
        m_sdbSizer7.AddButton( self.m_sdbSizer7OK )
        m_sdbSizer7.Realize();
        bSizer30.Add( m_sdbSizer7, 0, wx.EXPAND, 5 )
        
        self.SetSizer( bSizer30 )
        self.Layout()
        
        self.Centre( wx.BOTH )
        
        # Connect Events
        self.m_lcList.Bind( wx.EVT_LIST_ITEM_SELECTED, self.OnListItemSelected )
        self.m_btnNew.Bind( wx.EVT_BUTTON, self.OnNew )
        self.m_btnDelete.Bind( wx.EVT_BUTTON, self.OnDelete )
        self.m_btnEdit.Bind( wx.EVT_BUTTON, self.OnEdit )
        self.m_btnSubmit.Bind( wx.EVT_BUTTON, self.OnSubmit )
        
        self.currentitem = None
        self.dict = {}
        self._Init()
    
    
    # Virtual event handlers, overide them in your derived class
    def OnListItemSelected( self, event ):
        self.currentitem = item = event.m_itemIndex
        self.m_tcNo.SetValue(self.m_lcList.GetItemText(item))
        self.m_tcUserName.SetValue(self.m_lcList.GetItem(item, 1).GetText())
        p = self.m_lcList.GetItem(item, 2).GetText()
        self.m_tcPermission.SetStringSelection(p)
        event.Skip()
    
    def OnNew( self, event ):
        self.m_tcNo.SetValue(wx.EmptyString)
        self.m_tcUserName.SetValue(wx.EmptyString)
        self.m_tcUserName.Enable(True)
        self.m_tcPassword.SetValue(wx.EmptyString)
        self.m_tcPassword.Enable(True)
        self.m_tcSecondPassword.SetValue(wx.EmptyString)
        self.m_tcSecondPassword.Enable(True)
        self.m_tcPermission.SetSelection(0)
        self.m_tcPermission.Enable(True)
        event.Skip()
    
    def OnDelete( self, event ):
        if self.currentitem == None:
            wx.MessageBox(u"请选择需要删除的条目")
            return
        
        if PermissionVal[self.m_tcPermission.GetStringSelection()] == 0:
            res = Database.MUser.selectBy(permission = 0)
            if res.count() < 2:
                wx.MessageBox(u"系统中必需存在至少一个管理员用户")
                return
        
        if wx.MessageBox(u"你确定需要删除吗？一旦删除您将不能恢复", u"提示", wx.YES_NO) == wx.YES:
            index = int(self.m_tcNo.GetValue())
            Database.MUser.deleteBy(id = index)
            self.m_lcList.DeleteItem(self.currentitem)
            del self.dict[index]
            self.currentitem = None
            
            self.m_tcNo.SetValue(wx.EmptyString)
            self.m_tcUserName.SetValue(wx.EmptyString)
            self.m_tcUserName.Enable(False)
            self.m_tcPassword.SetValue(wx.EmptyString)
            self.m_tcPassword.Enable(False)
            self.m_tcSecondPassword.SetValue(wx.EmptyString)
            self.m_tcSecondPassword.Enable(False)
            self.m_tcPermission.SetSelection(0)
            self.m_tcPermission.Enable(False)
        event.Skip()
    
    def OnEdit( self, event ):
        if self.currentitem == None:
            wx.MessageBox(u"请选择需要删除的条目")
            return
        
        self.m_tcUserName.Enable(True)
        self.m_tcPassword.Enable(True)
        self.m_tcSecondPassword.Enable(True)
        self.m_tcPermission.Enable(True)
        event.Skip()
    
    def OnSubmit( self, event ):
        p1 = self.m_tcPassword.GetValue()
        if len(p1) == 0:
            wx.MessageBox(u"密码不能为空，请重新设置")
            self.m_tcPassword.SetFocus()
            return
        p2 = self.m_tcSecondPassword.GetValue()
        if p1 != p2:
            wx.MessageBox(u"确认密码不一致，请重新设置")
            self.m_tcSecondPassword.SetValue(wx.EmptyString)
            self.m_tcSecondPassword.SetFocus()
            return
        
        if self.m_tcPermission.GetStringSelection() == u"":
            wx.MessageBox(u"必需为用户指定权限")
            self.m_tcPermission.SetFocus()
            return
        
        if self.m_tcNo.GetValue() == wx.EmptyString:
            r = Database.MUser(name = self.m_tcUserName.GetValue(),
                                  password = self.m_tcPassword.GetValue(),
                                  permission = PermissionVal[self.m_tcPermission.GetStringSelection()])
            self._InsertItem(r)
        else:
            r = Database.MUser.get(id = int(self.m_tcNo.GetValue()))
            r.name = self.m_tcUserName.GetValue()
            r.password = self.m_tcPassword.GetValue().encode("ascii")
            r.permission = PermissionVal[self.m_tcPermission.GetStringSelection()]
            
            self._UpdateItem(r)
            
        self.m_tcUserName.Enable(False)
        self.m_tcPassword.Enable(False)
        self.m_tcSecondPassword.Enable(False)
        self.m_tcPermission.Enable(False)
        event.Skip()
    
    def _Init(self):
        collect = Database.MUser.select()
        for r in collect:
            self._InsertItem(r)
            
    def _InsertItem(self, r):
        item = self.m_lcList.InsertStringItem(self.m_lcList.GetItemCount(), "%d" % r.id)
        self.m_lcList.SetStringItem(item, 1, r.name)
        self.m_lcList.SetStringItem(item, 2, ValPermission[r.permission])
        self.dict[r.id] = item

    def _UpdateItem(self, r):
        item = self.dict[r.id]
        self.m_lcList.SetStringItem(item, 1, r.name)
        self.m_lcList.SetStringItem(item, 2, ValPermission[r.permission])